package com.zhoug.zmui3.autocomplete;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhoug.zmui3.R;


/**
 *
 * 自动补全提示列表数据适配器
 * @Author: zhoug
 * @Date: 2023-11-01
 * @Description:
 */
public class ZMUIAutoCompleteSimpleAdapter<T> extends ZMUIAutoCompleteAdapter<T> {
    private int gravity = Gravity.START | Gravity.CENTER_VERTICAL;

    public ZMUIAutoCompleteSimpleAdapter() {
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.zmui_auto_complete_simple_adapter_item, parent, false);
        }
        TextView tvName = convertView.findViewById(R.id.tv_text);
        ImageView ivSelectIcon = convertView.findViewById(R.id.iv_selectIcon);
        tvName.setGravity(gravity);
        T item = getItem(position);
        boolean selected = isSelected(item);
        //多选时选中显示图标
        if (isMultiSelect()) {
            //改变选中的文字颜色
            tvName.setSelected(selected);
            ivSelectIcon.setVisibility(selected ? View.VISIBLE : View.GONE);
        } else {
            ivSelectIcon.setVisibility(View.GONE);
        }
        if (item != null && getFilter() != null) {
            tvName.setText(getFilter().convertResultToString(item));
        } else {
            tvName.setText("");
        }
        return convertView;
    }


}
